// pages/add/dispatch/dispatch.js
import {
  areaList
} from '@vant/area-data';

Page({
  data: {
    bottomHeight: getApp().globalData.bottomHeight,
    fileList: [],
    imgIds: [],
    isDialog_choosetask: false,
    isDialog_des: false,
    isShowSlider: false,
    isShowArea: false,
    cityList: areaList,
    startTime: "",
    endTime: "",
    startTimeSecond: "",
    endTimeSecond: "",
    isStartDateTime: false,
    isEndDateTime: false,
    currentDateTime: "",
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      if (type === "hour") {
        return `${value}时`;
      }
      if (type === "minute") {
        return `${value}分`;
      }
      return value;
    },
    isPayList: false,
    areaList: [{
        id: 0,
        name: "不限"
      },
      {
        id: 1,
        name: "1公里内"
      },
      {
        id: 3,
        name: "3公里内"
      },
      {
        id: 5,
        name: "5公里内"
      },
    ],
    ageList: [],
    jobList: [],
    taskList: [],
    WCList: [],
    QQList: [],
    dayList: [{
        id: 0,
        name: "不限"
      },
      {
        id: 1,
        name: "1天"
      },
      {
        id: 2,
        name: "2天"
      }
    ],
    reawardList: [{
        id: 2,
        name: "2元"
      },
      {
        id: 4,
        name: "4元"
      },
      {
        id: 6,
        name: "6元"
      }
    ],
    currentTaskTmp: {},
    currentTask: {
      id: "0",
      name: "全部",
    },
    isReawardDiy: false,
    brand: "",
    title: "",
    content: "",
    area: "0",
    is_area_diy: false,
    area_diy: [],
    age: "0",
    social_group: "0",
    wechat_friends: "0",
    qq_friends: "0",
    display_days: "0",
    people_num: "",
    reaward: 2,
    service_fee: 0,
    service_percent: 0.05,
    reawardDiy: "",
    dispatch_fee: 0,
    checked: false,
    disabled: false,
    btn_status: true,
    isMask: false
  },
  onLoad: function (options) {
    this.getAgeList();
    this.getJobList();
    this.getTaskList();
    this.getWCList();
    this.getQQList();
  },
  getAgeList() {
    getApp().globalData.http.getBaseSelectOption({
      type: 1
    }).then(res => {
      this.setData({
        ageList: res.data
      });
    });
  },
  getJobList() {
    getApp().globalData.http.getBaseSelectOption({
      type: 2
    }).then(res => {
      this.setData({
        jobList: res.data
      });
    });
  },
  getTaskList() {
    getApp().globalData.http.getBaseSelectOption({
      type: 3
    }).then(res => {
      this.setData({
        taskList: res.data
      });
    });
  },
  getWCList() {
    getApp().globalData.http.getBaseSelectOption({
      type: 4
    }).then(res => {
      this.setData({
        WCList: res.data
      });
    });
  },
  getQQList() {
    getApp().globalData.http.getBaseSelectOption({
      type: 5
    }).then(res => {
      this.setData({
        QQList: res.data
      });
    });
  },
  taskHandle() {
    this.setData({
      currentTaskTmp: this.data.currentTask,
      isDialog_choosetask: true
    });
  },
  taskSel(e) {
    let tmp = {
      id: e.currentTarget.dataset.obj.id,
      name: e.currentTarget.dataset.obj.name,
    }
    this.setData({
      currentTaskTmp: tmp
    });
  },
  taskConfirm() {
    this.setData({
      currentTask: this.data.currentTaskTmp,
      isDialog_choosetask: false
    });
  },
  openDes() {
    this.setData({
      isDialog_des: true
    });
  },
  handleSlider() {
    this.setData({
      isShowSlider: !this.data.isShowSlider
    });
  },
  openArea() {
    this.setData({
      isShowArea: true
    });
  },
  areaCancel() {
    this.setData({
      isShowArea: false
    });
  },
  openStartDateTime() {
    this.setData({
      currentDateTime: new Date().getTime(),
      isStartDateTime: true
    });
  },
  startDateTimeConfirm(event) {
    let tmp = getApp().globalData.util.formatTime(new Date(event.detail)).split(":");
    this.setData({
      startTime: tmp[0] + ":" + tmp[1],
      startTimeSecond: event.detail,
      isStartDateTime: false
    });
  },
  startDateTimeCancel() {
    this.setData({
      isStartDateTime: false
    });
  },
  openEndDateTime() {
    this.setData({
      currentDateTime: new Date().getTime(),
      isEndDateTime: true
    });
  },
  endDateTimeConfirm(event) {
    let tmp = getApp().globalData.util.formatTime(new Date(event.detail)).split(":");
    this.setData({
      endTime: tmp[0] + ":" + tmp[1],
      endTimeSecond: event.detail,
      isEndDateTime: false
    });
  },
  endDateTimeCancel() {
    this.setData({
      isEndDateTime: false
    });
  },
  areaChange(event) {
    console.log(event.detail)
  },
  areaConfirm(event) {
    if (event.detail.index.includes(0)) {
      getApp().globalData.vant.toast("请选择所在地区");
    } else {
      this.setData({
        isShowArea: false,
        is_area_diy: true,
        area_diy: event.detail.values
      });
    }
  },
  handlePayList() {
    this.setData({
      isPayList: !this.data.isPayList
    });
  },
  upload(e) {
    getApp().globalData.util.imgsUpload(this, e, this.data.fileList, this.data.imgIds);
  },
  delete(e) {
    let i = e.detail.index;
    let fileList = this.data.fileList;
    let imgIds = this.data.imgIds;
    fileList.splice(i, 1);
    imgIds.splice(i, 1);
    this.setData({
      fileList: fileList,
      imgIds: imgIds
    });
  },
  areaSel(e) {
    this.setData({
      area: e.currentTarget.dataset.obj.id,
      is_area_diy: false
    });
  },
  ageSel(e) {
    this.setData({
      age: e.currentTarget.dataset.obj.id
    });
  },
  jobSel(e) {
    this.setData({
      social_group: e.currentTarget.dataset.obj.id
    });
  },
  wcSel(e) {
    this.setData({
      wechat_friends: e.currentTarget.dataset.obj.id
    });
  },
  qqSel(e) {
    this.setData({
      qq_friends: e.currentTarget.dataset.obj.id
    });
  },
  daySel(e) {
    this.setData({
      display_days: e.currentTarget.dataset.obj.id
    });
  },
  reawardSel(e) {
    this.setData({
      isReawardDiy: false,
      reaward: e.currentTarget.dataset.obj.id,
      service_fee: Math.ceil(this.data.people_num * e.currentTarget.dataset.obj.id * this.data.service_percent * 100) / 100,
      dispatch_fee: Math.ceil(this.data.people_num * e.currentTarget.dataset.obj.id * 100) / 100,
    });
  },
  reawardDiy() {
    this.setData({
      isMask: true,
      isReawardDiy: true,
      reawardDiy: this.data.reawardDiy.replace("元", "")
    });
  },
  numBlur() {
    let value1 = "";
    let value2 = "";
    if (this.data.isReawardDiy) {
      value1 = this.data.people_num * this.data.reawardDiy.replace("元", "");
      value2 = value1 * this.data.service_percent;
    } else {
      value1 = this.data.people_num * this.data.reaward;
      value2 = value1 * this.data.service_percent;
    }
    this.setData({
      dispatch_fee: Math.ceil(value1.toFixed(2) * 100) / 100,
      service_fee: Math.ceil(value2.toFixed(2) * 100) / 100
    });
  },
  reawardBlur() {
    if (!getApp().globalData.reg.price.test(this.data.reawardDiy)) {
      getApp().globalData.vant.toast("请输入正确的价格");
      this.setData({
        reawardDiy: "",
        dispatch_fee: 0,
        service_fee: 0,
        isMask: false
      });
    } else if (this.data.reawardDiy >= 10000) {
      getApp().globalData.vant.toast("目前任务奖励金额不能超过10000");
      this.setData({
        reawardDiy: ""
      });
    } else {
      this.setData({
        dispatch_fee: Math.ceil((this.data.people_num * this.data.reawardDiy).toFixed(2) * 100) / 100,
        service_fee: Math.ceil((this.data.people_num * this.data.reawardDiy * this.data.service_percent).toFixed(2) * 100) / 100,
        reawardDiy: !!this.data.reawardDiy ? this.data.reawardDiy + "元" : this.data.reawardDiy,
        isMask: false
      });
    }
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  submit() {
    let now = new Date().getTime();
    let params = {
      type: 4,
      title: this.data.title,
      content: this.data.content,
      imgIds: this.data.imgIds.join(","),
      taskOrderVoStr: {
        brand: this.data.brand,
        taskType: this.data.currentTask.id,
        // 区域距离
        distance: this.data.is_area_diy ? "" : this.data.area,
        // 区域自定义
        area: this.data.is_area_diy ? this.data.area_diy : "",
        ageGroup: this.data.age,
        socialGroup: this.data.social_group,
        wechatFriends: this.data.wechat_friends,
        qqFriends: this.data.qq_friends,
        displayDays: this.data.display_days,
        beginTimeStr: this.data.startTime,
        endTimeStr: this.data.endTime,
        peopleNum: this.data.people_num,
        reward: this.data.isReawardDiy ? this.data.reawardDiy.replace("元", "") : this.data.reaward,
        serviceFee: this.data.service_fee,
      },
    };
    if (this.data.btn_status) {
      if (!params.taskOrderVoStr.brand) {
        getApp().globalData.vant.toast("品牌不能为空");
      } else if (!params.title) {
        getApp().globalData.vant.toast("任务标题不能为空");
      } else if (!params.content) {
        getApp().globalData.vant.toast("任务内容不能为空");
      } else if (!params.imgIds) {
        getApp().globalData.vant.toast("您还没有上传图片");
      } else if (!params.taskOrderVoStr.beginTimeStr || !params.taskOrderVoStr.endTimeStr) {
        getApp().globalData.vant.toast("请选择派单有效期");
      } else if (now > params.taskOrderVoStr.beginTimeStr || now > params.taskOrderVoStr.endTimeStr) {
        getApp().globalData.vant.toast("派单时间不能早于当前时间");
      } else if (this.data.startTimeSecond >= this.data.endTimeSecond) {
        getApp().globalData.vant.toast("开始日期不能大于等于截止日期");
      } else if (!params.taskOrderVoStr.peopleNum) {
        getApp().globalData.vant.toast("派单人数不能为空");
      } else if (!this.data.checked) {
        getApp().globalData.vant.toast("请勾选邻小帮任务发布协议");
      } else {
        this.setData({
          disabled: true
        });
        params.taskOrderVoStr = JSON.stringify(params.taskOrderVoStr);
        getApp().globalData.http.squarePost(params).then(res => {
          getApp().globalData.util.pay(res.data, () => {
            this.setData({
              disabled: false
            });
            wx.reLaunch({
              url: "/pages/morejob/list/list",
            });
          }, () => {
            this.setData({
              disabled: false
            });
          });
        }).catch(e => {
          getApp().globalData.vant.toast.fail(e.msg);
        });
      }
    }
  }
})